package in.muton.edushare.components.tasks.requests;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.panels.paginator.components.badges.UserBadge;

import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.util.EntityUtil;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class RequestPanel extends EdusharePanel {

	private IModel<? extends IFileUploadable> entityModel;

	public RequestPanel(String id, final IModel<? extends IFileUploadable> model, final EduShareBasePage page) {
		super(id, model, page);
		this.entityModel = model;
		Form<Void> form = new Form<Void>("form");
		Button acceptAll = new Button("acceptAll"){
			@Override
			public void onSubmit() {
				List<Users> waitingUsers = getWaitingUsers();
				if(waitingUsers == null || waitingUsers.size()<1)
					return;
				for(Users u : waitingUsers){
					AcceptDeclineUtil.acceptDeclineUser(u, entityModel.getObject(), true);
				}
			}
		};
		form.add(acceptAll);
		Button declineAll = new Button("declineAll"){
			@Override
			public void onSubmit() {
				List<Users> waitingUsers = getWaitingUsers();
				if(waitingUsers == null || waitingUsers.size()<1)
					return;
				for(Users u : waitingUsers){
					AcceptDeclineUtil.acceptDeclineUser(u, entityModel.getObject(), false);
				}
			}
		};
		form.add(declineAll);
		
		ListView<Users> reqList = new ListView<Users>(
													"reqItem", 
													new PropertyModel<List<Users>>(RequestPanel.this, "waitingUsers")) {
			
			private static final long serialVersionUID = -8754522930274880281L;

			@Override
			protected void populateItem(final ListItem<Users> item) {
				item.add(new UserBadge("user", page, item.getModel()));
				SubmitLink accept = new SubmitLink("accept"){
					
					private static final long serialVersionUID = -5545770555366759022L;

					@Override
					public void onSubmit() {
						AcceptDeclineUtil.acceptDeclineUser(
											item.getModelObject(), 
											RequestPanel.this.entityModel.getObject(), 
											true);
					}
				};
				item.add(accept);
				
				SubmitLink decline = new SubmitLink("decline"){

					private static final long serialVersionUID = 4976270775005135567L;

					@Override
					public void onSubmit() {
						AcceptDeclineUtil.acceptDeclineUser(
											item.getModelObject(), 
											RequestPanel.this.entityModel.getObject(), 
											false);
					}
				};
				item.add(decline);
			}
			
			@Override
			protected void onBeforeRender() {
				if(getList()==null || getList().size()<1){
					RequestPanel.this.setVisible(false);
				}
				super.onBeforeRender();
			}
		};
		
		form.add(reqList);
		add(form);
	}
	
	public List<Users> getWaitingUsers(){
		return getRequestingUsers(entityModel);
	}

	private List<Users> getRequestingUsers(IModel<? extends IFileUploadable> model) {
		switch(EntityUtil.getEntityType(model.getObject())){
			
			case UNIVERSITY:
				//TODO:
				break;
			case DEPARTMENT:
				//TODO:
				break;
			case COURSE:
				//TODO:
				break;
			case GROUP:
				return new GroupsMembersDAO(getORMInterface()).getMembers((Groups)model.getObject(), GroupMembers.STATUS_WAITING_APPROVAL);
			case USER:
				//TODO:
				break;
			default:
				break;
		}
		return null;
	}

}
